#include "GLTexture.h"


GLTexture::GLTexture()
{
    textureId = 0;
    width = 0;
    height = 0;
}

GLTexture::GLTexture(int w, int h)
{
    glGenTextures(1, &(textureId));
    glBindTexture(GL_TEXTURE_2D, textureId);
    glEnable(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h,
                 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
    width = w;
    height = h;
}

GLTexture::~GLTexture()
{
    if (textureId)
		glDeleteTextures(1, &textureId);
}


//
// constructors
//
#ifdef GLTEX_LOADER_ENABLED

GLTexture* GLTexture::createFromFile(const char *filename, int flags)
{
	GLTexture* tex = NULL;
    UTImage *img = new UTImage(filename);
    if (img->loaded)
    {
        unsigned int type = (img->bpp == 32) ? GL_RGBA : GL_RGB;
    	tex = new GLTexture();

        glGenTextures(1, &tex->textureId);
        glBindTexture(GL_TEXTURE_2D, tex->textureId);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        if (flags == GLTEX_EDGE)
        {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        }

        gluBuild2DMipmaps(GL_TEXTURE_2D, type, img->width, img->height, type,
                          GL_UNSIGNED_BYTE, img->data);
        tex->fileName = img->fileName;
        tex->width = img->width;
        tex->height = img->height;
        printf("NEW RESOURCE: Texture '%s'\n", filename);
    }
    else
    {
        printf("NOT FOUND: Texture '%s'\n", filename);
    }
    delete img;
    return tex;
}

#endif // GLTEX_LOADER_ENABLED


void GLTexture::drawBillboard(float x1, float y1, float x2, float y2)
{
	enable();
    glBegin(GL_QUADS);
        //normal
        glNormal3f(0.0f, 0.0f, 1.0f);
        //verteces
        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(x1, y1);
        glTexCoord2f(1.0f, 0.0f);
        glVertex2f(x2, y1);
        glTexCoord2f(1.0f, 1.0f);
        glVertex2f(x2, y2);
        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(x1, y2);
    glEnd();
    disable();
}

void GLTexture::drawImage(float x1, float y1, float x2, float y2)
{
    enable();
    glBegin(GL_QUADS);
        glNormal3f(0.0f, 0.0f, 1.0f);
        glTexCoord2f(0.0f, 0.0f);
        glVertex2i(x1, y1);
        glTexCoord2f(0.0f, 1.0f);
        glVertex2i(x1, y2);
        glTexCoord2f(1.0f, 1.0f);
        glVertex2i(x2, y2);
        glTexCoord2f(1.0f, 0.0f);
        glVertex2i(x2, y1);
    glEnd();
    disable();

}

void GLTexture::update(void* data)
{
    enable();
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA,
                    GL_UNSIGNED_INT_8_8_8_8_REV, data);
    disable();
}


